import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'
import Layout from '@/layout/layout.vue'

export const constantRoutes: Array<RouteRecordRaw> = [
  {
    path: '/',
    component: () => import('@/views/home/index.vue'),
    meta: { hidden: true }
  },
  {
    path: '/dashboard',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: '',
        component: () => import('@/views/dashboard/index.vue'),
        name: 'Dashboard',
        meta: { title: '控制台', icon: 'dashboard', inLayout: true }
      }
    ]
  },
  {
    path: '/login',
    component: () => import('@/views/login/login.vue'),
    meta: { hidden: true }
  },
  {
    path: '/register',
    component: () => import('@/views/login/register.vue'),
    meta: { hidden: true }
  },
  {
    path: '/forgot-password',
    component: () => import('@/views/login/ForgotPassword.vue'),
    meta: { hidden: true }
  },
  {
    path: '/404',
    component: () => import('@/views/error/404.vue'),
    meta: { hidden: true }
  },
  {
    path: '/user/profile',
    component: Layout,
    meta: { hidden: true },
    children: [
      {
        path: '',
        component: () => import('@/views/user/profile.vue'),
        name: 'Profile',
        meta: { title: '个人中心', hidden: true, inLayout: true }
      }
    ]
  },
  {
    path: '/test',
    component: Layout,
    meta: { hidden: true },
    children: [
      {
        path: 'result/:id',
        component: () => import('@/views/test/test-result.vue'),
        name: 'TestResult',
        meta: { title: '测试结果', hidden: true, inLayout: true }
      }
    ]
  },
]

const router = createRouter({
  history: createWebHistory(),
  routes: constantRoutes,
  scrollBehavior: () => ({ top: 0 })
})

// 动态添加路由后再添加404路由
export function add404Route() {
  router.addRoute({
    path: '/:pathMatch(.*)*',
    name: 'NotFound',
    component: () => import('@/views/error/404.vue'),
    meta: { hidden: true }
  })
}

// 重置路由方法
export function resetRouter() {
  const newRouter = createRouter({
    history: createWebHistory(),
    routes: constantRoutes
  })

  // @ts-ignore
  router.matcher = newRouter.matcher
}

export default router